package cn.laixueit.fileupload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileController {


    @RequestMapping("/upload")
    public String upload(MultipartFile img, HttpServletRequest request) throws IOException {
//        System.out.println("img.getName() = " + img.getName());
//        System.out.println("img.getOriginalFilename() = " + img.getOriginalFilename());
        String realPath = request.getSession().getServletContext().getRealPath("/img");
        File file = new File(realPath);
        if(!file.exists()){
            file.mkdirs();
        }
        File newFile = new File(file, UUID.randomUUID().toString().replace("-", "").substring(8) + img.getOriginalFilename());
        img.transferTo(newFile);
        return "index";
    }

}
